#!/usr/bin/env python3
# -*- coding: utf-8 -*-


class MyObject(object):

    def __init__(self):
        self.x = 9

    def power(self):
        return self.x * self.x


obj = MyObject()
print("dir(): ",dir(obj) )

print("hasattr(obj, 'x') =", hasattr(obj, "x"))  # 是否有属性x
print("hasattr(obj, 'y') =", hasattr(obj, "y"))  # 是否有属性y
setattr(obj, "y", 19)  # 设置属性
print("hasattr(obj, 'y') =", hasattr(obj, "y"))  # 加上了y属性
print("getattr(obj, 'y') =", getattr(obj, "y"))  # 获取值
print("obj.y =", obj.y)  # 获取值

print("getattr(obj, 'z') =", getattr(obj, "z", 404))  # 没有就设置默认值

f = getattr(obj, "power")  # 获取属性
print(f)
print(f()) # 调用函数power(),实现9的平方